<template>
	<div ref="dom" class="charts chart-pie"></div>
</template>

<script>
import * as echarts from 'echarts'
import tdTheme from './theme.json'
import { on, off } from '@/libs/tools'
// echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'ChartPie',
  props: {
    value: Array,
    text: String,
    subtext: String
  },
  data () {
    return {
      dom: null
    }
  },
  watch: {
    value: {
      deep: true,
      handler (val) {
        this.intChart()
      }
    }
  },
  methods: {
    resize () {
      this.dom.resize()
    },
    intChart () {
      let colorList = [
        '#5470c6',
        '#91cc75',
        '#fac858',
        '#ee6666',
        '#73c0de',
        '#3ba272',
        '#fc8452',
        '#9a60b4',
        '#ea7ccc'
      ]
      this.$nextTick(() => {
        let legend = this.value.map(_ => _.name)
        let option = {
          color: colorList,
          title: {
            text: this.text,
            subtext: this.subtext,
            x: 'center'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 0,
            top: '1%',
            bottom: '1%'
          },
          grid: {
            top: '5%',
            bottom: '5%',
            left: '5%',
            right: '5%'
          },
          series: [
            {
              name: '合作学者分布',
              type: 'pie',
              radius: ['35%', '55%'],
              // roseType: 'radius',
              center: ['35%', '50%'],
              // roseType: 'radius',
              itemStyle: {
                borderRadius: 5
              },
              label: {
                show: false
                // formatter: params=>{
                //   if (this.value.length>=10){
                //     if (params.dataIndex<10){
                //       params.data.label = {position: 'outside'}
                //       params.data.labelLine = {
                //         show:true,

                //         // moveOverlap: true
                //         // length: 3000,
                //         // smooth: true
                //       }
                //       return params.name + ':' + params.percent + '%'
                //     } else {
                //       return ''
                //     }
                //   } else{
                //     params.data.label = {position: 'outside'}
                //     params.data.labelLine = {show:true}
                //     return params.name + '\n' + params.percent + '%'
                //   }
                // },
                // position: "inside",
              },
              labelLine: {
                show: false
              },
              data: this.value,
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
        this.dom = echarts.init(this.$refs.dom, 'tdTheme')
        this.dom.setOption(option)
        on(window, 'resize', this.resize)
      })
    }
  },
  mounted () {
    this.intChart()
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  }
}
</script>
